//
//  PushTableViewController.m
//  FangXinLang
//
//  Created by AJY on 16/9/18.
//  Copyright © 2016年 AJY. All rights reserved.
//

#import "PushTableViewController.h"
#import <AFNetworking.h>
#import "UIButton+Button.h"
#import "PushModel.h"
#import <UIImageView+WebCache.h>
#import "HTableViewCell.h"
#import "FTableViewCell.h"
#import "WebViewController.h"
#import "HViewController.h"
@interface PushTableViewController ()
@property(nonatomic,strong)NSMutableArray *arrC;
@property(nonatomic,copy)NSString *pict;
@end

@implementation PushTableViewController
-(NSMutableArray *)arrC{
    if(!_arrC)
    {
        _arrC=[NSMutableArray array];
    }
    return _arrC;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.contentInset=UIEdgeInsetsMake(-20, 0, 0, 0);
    //self.navigationController.navigationBar.hidden=YES;
        self.navigationController.navigationBar.alpha=0;
    UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame=CGRectMake(0, 0, 100, 44);
    [btn setTitle:@"今日推送" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"white_back_ico_normal"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item=[[UIBarButtonItem alloc]initWithCustomView:btn];
    self.navigationItem.leftBarButtonItem=item;
    
    [self addData];
    
    
}
-(void)back{
    
    self.navigationController.navigationBar.alpha=1;
    [self.navigationController popViewControllerAnimated:YES];
}
///获取数据
-(void)addData{
    AFHTTPSessionManager *manage=[AFHTTPSessionManager manager];
    [manage GET:@"http://newsapi.sina.cn/?resource=msg/list&deviceId=cf3a669aaccc85e5&from=6053295012&weiboUid=&weiboSuid=&imei=861717030685322&wm=b207&chwm=14020_0001&oldChwm=14020_0001&osVersion=5.1&connectionType=2&resolution=720x1184&city=CHXX0122&deviceModel=HUAWEI__HUAWEI__HUAWEI+TIT-AL00&location=0.0%2C0.0&link=&mac=bc%3A75%3A74%3Aab%3Abb%3A77&ua=HUAWEI-HUAWEI+TIT-AL00__sinanews__5.3.2__android__5.1&urlSign=953c4202c4&rand=169" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //[responseObject writeToFile:@"/Users/ajy/Desktop/tuisong.plist" atomically:YES];
        self.pict=responseObject[@"data"][@"focus"][@"activityDayIconUrl"];
        NSArray *arr=responseObject[@"data"][@"list"];
        for (NSDictionary *dic in arr) {
            PushModel *model=[PushModel pushmodelWith:dic];
            [self.arrC addObject:model];
        }
        if(self.arrC)
        {
            [self.tableView reloadData];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section==0)
    {
        return 1;
    }else{
        return self.arrC.count;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = nil;
    if(indexPath.section==0)
    {
        self.tableView.rowHeight=200;
    static NSString *ID=@"cell";
        HTableViewCell *cellH=[tableView dequeueReusableCellWithIdentifier:ID];
        if(cellH==nil)
        {
            cellH=[HTableViewCell htableviewcell];
        }
        [cellH.img sd_setImageWithURL:[NSURL URLWithString:self.pict]];
        cell=cellH;
    }else{
        self.tableView.rowHeight=60;
    static NSString *ID=@"Cell";
        FTableViewCell *cellF=[tableView dequeueReusableCellWithIdentifier:ID];
        if(cellF==nil)
        {
            cellF=[FTableViewCell ftableviewcell];
        }
        PushModel *model=self.arrC[indexPath.row];
        cellF.title.text=model.title;
        cellF.subtitle.text=model.content;
        cellF.num.text=[NSString stringWithFormat:@"%@",model.cmntCount];
        cell=cellF;
        
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section==0)
    {
        HViewController *hv=[[HViewController alloc]init];
        hv.hidesBottomBarWhenPushed=YES;
        [self.navigationController pushViewController:hv animated:YES];
    
    }else{
        PushModel *model=self.arrC[indexPath.row];
        WebViewController *web=[[WebViewController alloc]init];
        web.url=model.url;
        web.hidesBottomBarWhenPushed=YES;
        [self.navigationController pushViewController:web animated:YES];
        
        
    }
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat y=self.tableView.contentOffset.y-64;
    if(y>0)
    {
        self.navigationController.navigationBar.alpha=y/50;
    }

    }
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
